package com.ruoyi.system.controller;

import com.ruoyi.common.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SaveBusinessVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZnUserBusiness;
import com.ruoyi.system.service.IZnUserBusinessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户模块关系Controller
 * 
 * @author ruoyi
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/system/business")
public class ZnUserBusinessController extends BaseController
{
    @Autowired
    private IZnUserBusinessService znUserBusinessService;

    /**
     * 查询用户模块关系列表
     */
    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZnUserBusiness znUserBusiness)
    {
        startPage();
        List<ZnUserBusiness> list = znUserBusinessService.selectZnUserBusinessList(znUserBusiness);
        return getDataTable(list);
    }

    /**
     * 导出用户模块关系列表
     */
    @PreAuthorize("@ss.hasPermi('system:business:export')")
    @Log(title = "用户模块关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnUserBusiness znUserBusiness)
    {
        znUserBusiness.setTenantId(SecurityUtils.getTenantId());
        List<ZnUserBusiness> list = znUserBusinessService.selectZnUserBusinessList(znUserBusiness);
        ExcelUtil<ZnUserBusiness> util = new ExcelUtil<ZnUserBusiness>(ZnUserBusiness.class);
        util.exportExcel(response, list, "用户模块关系数据");
    }

    /**
     * 获取用户模块关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(znUserBusinessService.selectZnUserBusinessById(id));
    }

    /**
     * 新增用户模块关系
     */
    @PreAuthorize("@ss.hasPermi('system:business:add')")
    @Log(title = "用户模块关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnUserBusiness znUserBusiness)
    {
        return toAjax(znUserBusinessService.insertZnUserBusiness(znUserBusiness));
    }

    /**
     * 修改用户模块关系
     */
    @PreAuthorize("@ss.hasPermi('system:business:edit')")
    @Log(title = "用户模块关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnUserBusiness znUserBusiness)
    {
        return toAjax(znUserBusinessService.updateZnUserBusiness(znUserBusiness));
    }

    /**
     * 删除用户模块关系
     */
    @PreAuthorize("@ss.hasPermi('system:business:remove')")
    @Log(title = "用户模块关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(znUserBusinessService.deleteZnUserBusinessByIds(ids));
    }
    /**
     * 设置用户模块关系
     */
    @PostMapping("/saveUserBusiness")
    public AjaxResult saveUserBusiness(@RequestBody SaveBusinessVO saveBusinessVO)
    {
        if(saveBusinessVO.getValueList()==null||saveBusinessVO.getValueList().size()==0){
            return AjaxResult.error("未添加数据");
        }
        return toAjax(znUserBusinessService.saveUserBusiness(saveBusinessVO));
    }
    /**
     * 查询用户模块关系列表
     */
    @GetMapping("/listValues")
    public AjaxResult listValues(SaveBusinessVO saveBusinessVO)
    {
        String type=null;
        if(saveBusinessVO.getType().equals("1")){
            type="UserDepot";
        }else if(saveBusinessVO.getType().equals("2")){
            type="UserCustomer";
        }else {
            return AjaxResult.error("type值错误");
        }

        saveBusinessVO.setType(type);
        saveBusinessVO.setTenantId(SecurityUtils.getTenantId());
        ZnUserBusiness znUserBusiness = znUserBusinessService.selectZnUserBusinessByUserIdAndType(saveBusinessVO);
        List<Long> values=new ArrayList<>();
        if(null!=znUserBusiness&&znUserBusiness.getValue()!=null){
         String[] strs=   znUserBusiness.getValue().split(",");
         for(String str:strs){
             values.add(Long.parseLong(str));
         }
        }

        return AjaxResult.success(values);
    }

}
